package org.cyz.tools.service;

import cn.hutool.core.util.StrUtil;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import javafx.application.Platform;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import org.cyz.tools.Application;
import org.cyz.tools.common.bean.DBInfo;
import org.cyz.tools.common.util.ConsoleUtil;
import org.cyz.tools.common.util.RedisUtil;
import org.cyz.tools.common.util.sqlite.SQLiteUtil;
import org.cyz.tools.controller.MainController;
import org.cyz.tools.core.ContextManager;
import org.cyz.tools.core.assembly.Notify;
import org.cyz.tools.core.assembly.toast.Toast;
import org.cyz.tools.core.manager.WindowManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewConnectionService  {

   
    public JFXComboBox connTimeout;
    public JFXComboBox execTimeout;
    public JFXComboBox findDatabaseTimeout;
    public TextField urlNameText;
    public TextField addressUrlText;
    public TextField portText;
    public TextField passWordText;
    public TextField userNameText;

    /**
     * SSL-TLS
     **/
    public JFXButton pubKeyBtn;
    public TextField pubKey;
    public JFXButton authKeyBtn;
    public TextField authKey;

    public JFXButton priKeyBtn;
    public TextField priKey;

    public JFXButton testButton;
    public JFXButton acceptButton;
   
    public JFXButton closeButton;
    /**
     * ssh方式授权
     **/
   
    public JFXButton sshPubKeyBtn;
    public JFXButton sshPriKeyBtn;
    
    
    private ConsoleUtil consoleUtil=new ConsoleUtil();
    private RedisUtil redisUtil=new RedisUtil();

    public NewConnectionService(JFXComboBox connTimeout, JFXComboBox execTimeout, JFXComboBox findDatabaseTimeout, TextField urlNameText, TextField addressUrlText, TextField portText, TextField passWordText, TextField userNameText, JFXButton pubKeyBtn, TextField pubKey, JFXButton authKeyBtn, TextField authKey, JFXButton priKeyBtn, TextField priKey, JFXButton testButton, JFXButton acceptButton, JFXButton closeButton, JFXButton sshPubKeyBtn, JFXButton sshPriKeyBtn) {
        this.connTimeout = connTimeout;
        this.execTimeout = execTimeout;
        this.findDatabaseTimeout = findDatabaseTimeout;
        this.urlNameText = urlNameText;
        this.addressUrlText = addressUrlText;
        this.portText = portText;
        this.passWordText = passWordText;
        this.userNameText = userNameText;
        this.pubKeyBtn = pubKeyBtn;
        this.pubKey = pubKey;
        this.authKeyBtn = authKeyBtn;
        this.authKey = authKey;
        this.priKeyBtn = priKeyBtn;
        this.priKey = priKey;
        this.testButton = testButton;
        this.acceptButton = acceptButton;
        this.closeButton = closeButton;
        this.sshPubKeyBtn = sshPubKeyBtn;
        this.sshPriKeyBtn = sshPriKeyBtn;
    }

    public void initListener() {
        Map<String, String> maps = new HashMap<>();
        maps.put("PEM", "*.PEM");
        pubKeyBtn.setOnMouseClicked(mouseEvent -> {
            String path = new org.cyz.tools.core.assembly.FileChooser().setTitle("选择授权公钥文件").setInitPath(System.getProperty("user.home")).setStage(Application.stage).setExtensions(maps).builder();
            if (null != path) {
                pubKey.setText(path);
            }
        });
        priKeyBtn.setOnMouseClicked(mouseEvent -> {
            String path = new org.cyz.tools.core.assembly.FileChooser().setTitle("选择授权私钥文件").setInitPath(System.getProperty("user.home")).setStage(Application.stage).setExtensions(maps).builder();
            if (null != path) {
                priKey.setText(path);
            }
        });
        authKeyBtn.setOnMouseClicked(mouseEvent -> {
            String path = new org.cyz.tools.core.assembly.FileChooser().setTitle("选择授权认证文件").setInitPath(System.getProperty("user.home")).setStage(Application.stage).setExtensions(maps).builder();
            if (null != path) {
                authKey.setText(path);
            }
        });
        testButton.setOnMouseClicked(mouseEvent -> {
            if(StrUtil.isEmpty(addressUrlText.getText())){
                Notify.toast("请输入服务器地址!");
                return ;
            }
            if(StrUtil.isEmpty(portText.getText())){
                Notify.toast("端口不能为空且为正整数!");
                return;
            }
           boolean flag= redisUtil.testConnectRedis(addressUrlText.getText(),portText.getText());
            if(flag){
                Platform.runLater(()->{
//                    Toast_back.toast("连接成功!");
//                   Toast.builder().title("恭喜").message("连接成功!").delay_time(3).type(MonologFXButton.Type.OK).build().create();
                    Notify.notify("提示", "连接可用!", 1);
                });
            }
        });
        closeButton.setOnMouseClicked(mouseEvent -> {
            closeWin(mouseEvent);
        });
        acceptButton.setOnMouseClicked(mouseEvent -> {
            //连接名称
            String urlName=urlNameText.getText();
            //远程地址
            String serverUrl=addressUrlText.getText();
            String passWord=passWordText.getText();
            String userName=userNameText.getText();
            int port= Integer.parseInt(portText.getText());
            DBInfo dbInfo=new DBInfo();
            dbInfo.setUrlName(urlName);
            dbInfo.setHost(serverUrl);
            dbInfo.setPort(port);
            dbInfo.setUserName(userName);
            dbInfo.setPassWord(passWord);
            if(SQLiteUtil.tableIsExist("db_info")){
                SQLiteUtil.createTableIfNotExits(DBInfo.class);
            }
            if(SQLiteUtil.save(dbInfo)>0){
                closeWin(mouseEvent);
                //重新改变redis的连接
                reload();
                Platform.runLater(()->{
                    Notify.notify("提示", "添加成功!", 0);
                });
            }
        });
    }

    public void initData() {
        //连接超时设置
        List<Integer> items = new ArrayList<>();
        items.add(10);
        items.add(30);
        items.add(60);
        items.add(90);
        items.add(120);
        connTimeout.getItems().addAll(items);
        execTimeout.getItems().addAll(items);
        findDatabaseTimeout.getItems().addAll(items);
    }

    private void reload() {
        MainController mc = (MainController) ContextManager.ctrManager.getController(MainController.class.getSimpleName());
        mc.initMenu();
    }



    /*******************测试数据***********************/
    public void testData(){
        urlNameText.setText("我的redis服务器");
        addressUrlText.setText("127.0.0.1");
        portText.setText("6379");
    }

    /**
     * 退出系统
     *
     * @param mouseEvent
     */
    public void closeWin(MouseEvent mouseEvent) {
        WindowManager.stage.close();
    }
}
